package com.assjirc.commands;

import java.util.Arrays;
import java.util.Map;

import com.assjirc.annotations.Command;
import com.assjirc.frames.GUI;

@Command("help")
public class Help implements Executable {

	@Override
	public void execute(String params, GUI gui) {
		String help = null;
		if (params == null) {
			help = getSimpleHelp();
		} else {
			params = params.trim();
			Map<String, Object> map = gui.getCommandMap();

			if (params.equalsIgnoreCase("commands")) {
				if (map != null && !map.isEmpty()) {
					String[] commands = map.keySet().toArray(new String[0]);
					Arrays.sort(commands);
					help = "";
					for (String command : commands) {
						if (!command.equalsIgnoreCase("help")) {
							Executable exec = (Executable) map.get(command);
							help += "\n/" + command + ": " + exec.getSimpleHelp();
						}
					}
				}
			} else {
				if (params.contains(" ")) { // has params to pass to help
					int index = params.indexOf(" ");
					String command = params.substring(0, index);
					params = params.substring(index + 1);
					Executable exec = (Executable) map.get(command);
					help = "\n" + command + ": " + exec.getDetailedHelp(params);
				} else { // get command details
					Executable exec = (Executable) map.get(params);
					help = "\n" + params + ": " + exec.getDetailedHelp(params);
				}
			}
		}

		gui.getChannelTextArea().append(help);
	}

	@Override
	public String getDetailedHelp(String... params) {
		return getSimpleHelp();
	}

	@Override
	public String getSimpleHelp() {
		String help = "\nto get a listing of all commands, type /help commands";
		help += "\nto get help for a specific command, type /help <command>";
		return help;
	}

}
